package com.yjd.back.system.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.ReginTypeEnum;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.service.ISysParamService;
import com.yjd.comm.util.FrameUtil;

/**
 * <b>description</b>：操作记录 <br>
 * <b>time</b>：2014-11-7上午10:30:55 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Controller
@Scope("prototype")
@RequestMapping("/sysparam/")
public class SysParamControl extends BaseControl {

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("list")
	public String list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<SysParamModel> list = this.sysParamService.getModelList(FrameUtil.newHashMap("order"," order by id_param desc"),DbWREnums.READ);
		for (SysParamModel sysParamModel : list) {
			if (sysParamModel.getValue_type() != null) {
				for (ReginTypeEnum reginTypeEnum : ReginTypeEnum.values()) {
					if (reginTypeEnum.getValue().equals(
							sysParamModel.getValue_type())) {
						sysParamModel.getStrMap().put("value_type",
								reginTypeEnum.getDescription());
					}
				}
			}
		}
		request.setAttribute("list", list);
		return "sysparam.list";
	}

	/**
	 * 跳转到新增页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toadd")
	public String toadd() throws Exception {
		this.addOrEditInit();
		return "sysparam.toaddoredit";
	}

	private void addOrEditInit() throws Exception {
		request.setAttribute("reginTypeList", ReginTypeEnum.values());
	}

	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toedit/{id_param}")
	public String toedit(@PathVariable("id_param") Long id_param)
			throws Exception {
		if (id_param == null) {
			FrameUtil.throwBaseException1("参数有误!");
		}
		SysParamModel model = this.sysParamService.getModelById(id_param,DbWREnums.READ);
		if (model == null) {
			return this.toErrorPage(null, "数据被删除!");
		}
		this.addOrEditInit();
		request.setAttribute("model", model);
		return "sysparam.toaddoredit";
	}

	/**
	 * 新增和保存
	 * 
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(SysParamModel model) throws Exception {
		if (model.getId_param() == null) {
			this.sysParamService.insert(model, this.getOperParamDto());
		} else {
			this.sysParamService.update(model, this.getOperParamDto());
		}
		this.successMsg(null, "保存成功!");
	}

	/**
	 * 根据管理员id删除管理员
	 * 
	 * @param lid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("del/{id_param}")
	public void del(@PathVariable("id_param") Long id_param) throws Exception {
		if (id_param == null) {
			FrameUtil.throwBaseException1("参数有误!");
		}
		this.sysParamService.delete(id_param, this.getOperParamDto());
		this.successMsg(null, "删除数据成功!");
	}

	@Resource
	private ISysParamService sysParamService;
}
